﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SchemaRepositoryTests.cs" company="Data Ductus Malmö">
//   (c) 2010 - 2012 Data Ductus Malmö AB
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace DataDuctus.Snmp.Persistor.Core.Tests
{
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;

  using DataDuctus.Snmp.EmbeddableNetSnmp;
  using DataDuctus.Snmp.Persistor.Core.Schema;

  using Microsoft.VisualStudio.TestTools.UnitTesting;

  /// <summary>
  /// Test queries towards the schema repository.
  /// </summary>
  [TestClass]
  public class SchemaRepositoryTests
  {
    #region Public Properties

    /// <summary>
    /// Gets or sets the test context which provides
    /// information about and functionality for the current test run.
    /// </summary>
    public TestContext TestContext { get; set; }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    /// The test schema join.
    /// </summary>
    [TestMethod]
    public void TestSchemaJoin()
    {
      var repos = new SchemaRepository();

      var mibPath = Path.Combine(Directory.GetCurrentDirectory(), "mibs");

      EmbeddableSnmpAgent.ExtractMibs(mibPath);

      repos.LoadModules(mibPath, "*.txt");

      Console.WriteLine("Dump all tables under mib-2 branch");
      IOrderedEnumerable<SchemaRepositoryDsTable> mib2Tables = from ift in repos.DataSource.Tables
                                                               where
                                                                 ift.Namespace.StartsWith(
                                                                   "iso.org.dod.internet.mgmt.mib-2")
                                                               orderby ift.Name
                                                               select ift;

      foreach (SchemaRepositoryDsTable tbl in mib2Tables)
      {
        Console.WriteLine(tbl.Name);
      }

      Console.WriteLine("Dump all tables not under mib-2 branch");
      IOrderedEnumerable<SchemaRepositoryDsTable> nonMib2Tables = from ift in repos.DataSource.Tables
                                                                  where
                                                                    !ift.Namespace.StartsWith(
                                                                      "iso.org.dod.internet.mgmt.mib-2")
                                                                  orderby ift.Name
                                                                  select ift;

      foreach (SchemaRepositoryDsTable tbl in nonMib2Tables)
      {
        Console.WriteLine(tbl.Name);
      }

      Console.WriteLine("Join ifTable and ifXTable");
      IEnumerable<SchemaRepositoryDsView> ifandifxtable = from ift in repos.DataSource.Tables
                                                          where ift.Name == "ifTable"
                                                          from ifxt in repos.DataSource.Tables
                                                          where ifxt.Name == "ifXTable"
                                                          select
                                                            new SchemaRepositoryDsView
                                                              {
                                                                Name = "JoinedIfIfX", 
                                                                Namespace = "custom", 
                                                                Columns = ift.Columns.Union(ifxt.Columns)
                                                              };

      SchemaRepositoryDsTable jififx = ifandifxtable.First();

      Console.WriteLine(string.Format("{0}, col cnt: {1}", jififx.Name, jififx.Columns.Count));

      foreach (ISchemaItem item in jififx.Columns)
      {
        Console.WriteLine("{0}\t\t{1}", item.Name, item.Oid);
      }
    }

    #endregion
  }
}